import '../log/LogUtil.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionUtil{

  static Future<bool> isLocationServiceOn() async{
    return await Permission.location.serviceStatus.isEnabled;
  }

  static bool hasPermission = false;
  static Future<bool> checkPermissions(List<Permissions> permissions) async{
    if(hasPermission){
      return true;
    }
    Map<Permission, PermissionStatus> statuses =
    await permissions.map((e) => e.value).toList().request();
    hasPermission = true;
    statuses.forEach((key, value) {
      LogUtil.println('$key $value');
      if(!value.isGranted){
        hasPermission = false;
        return;
      }
    });
    return hasPermission;
  }

  static toAppSetting() {
    openAppSettings();
  }
}

enum Permissions{
  phone(Permission.phone),
  sms(Permission.sms),
  contacts(Permission.contacts),
  calendar(Permission.calendarFullAccess);

  final Permission value;
  const Permissions(this.value);
}